package com.openyelp.actions.shop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.openyelp.data.core.Pagination;
import com.openyelp.data.entity.Shop;
import com.openyelp.data.entity.ShopReview;
import com.openyelp.data.service.ShopReviewService;
import com.openyelp.data.service.ShopService;
import com.openyelp.shiro.utils.UserUtil;
import com.openyelp.web.utils.FrontUtils;

@Controller
@RequestMapping(value = "shop")
public class ShopFrontAction {
	@Autowired
	private ShopService shopService;
	@Autowired
	private ShopReviewService shopReviewService;

	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(
			int id,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Shop shop = shopService.findById(id);
		model.addAttribute("shop", shop);
		Pagination rs = shopReviewService.pageByShop(id, curpage, pagesize);
		if (rs != null) {
			model.addAttribute("list", rs.getList());
			model.addAttribute("page", rs);
		}
		model.addAttribute("curpage", curpage);
		model.addAttribute("id", id);
		model.addAttribute("pagesize", pagesize);
		return FrontUtils.getPath("shop/view");
	}

	@RequestMapping(value = "findbytype", method = RequestMethod.GET)
	public String findbytype(
			int id,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Pagination rs = shopService.page(curpage, pagesize);
		if (rs != null) {
			model.addAttribute("list", rs.getList());
			model.addAttribute("page", rs);
		}
		model.addAttribute("curpage", curpage);
		model.addAttribute("id", id);
		model.addAttribute("pagesize", pagesize);
		return FrontUtils.getPath("shop/findbytype");
	}

	@RequestMapping(value = "photos", method = RequestMethod.GET)
	public String photos(
			int id,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Shop shop = shopService.findById(id);
		model.addAttribute("shop", shop);

		return FrontUtils.getPath("shop/photos");
	}

	@RequestMapping(value = "writeareview", method = RequestMethod.GET)
	public String writeareview(
			int id,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Shop shop = shopService.findById(id);
		model.addAttribute("shop", shop);

		return FrontUtils.getPath("shop/writeareview");
	}

	@RequestMapping(value = "commentshop", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String comment(
			ShopReview shopReview,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		shopReview.setUser(UserUtil.getCurrentUser());
		shopReviewService.save(shopReview);

		return FrontUtils.redirect("/shop/view.htm?id="
				+ shopReview.getShop().getId());
	}

	@RequestMapping(value = "user_photos", method = RequestMethod.GET)
	public String user_photos(
			int id,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Shop shop = shopService.findById(id);
		model.addAttribute("shop", shop);

		return FrontUtils.getPath("shop/user_photos");
	}

	@RequestMapping(value = "user_photos_upload_finished", method = RequestMethod.GET)
	public String user_photos_upload_finished(
			int id,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Shop shop = shopService.findById(id);
		model.addAttribute("shop", shop);

		return FrontUtils.getPath("shop/user_photos_upload_finished");
	}

	@RequestMapping(value = "send_to_friend", method = RequestMethod.GET)
	public String send_to_friend(
			int id,
			@RequestParam(value = "curpage", required = true, defaultValue = "1") int curpage,
			@RequestParam(value = "pagesize", required = true, defaultValue = "10") int pagesize,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Shop shop = shopService.findById(id);
		model.addAttribute("shop", shop);

		return FrontUtils.getPath("shop/send_to_friend");
	}

}
